/**
 * Created by Simon on 2016/2/27.
 */

/*弹出收起*/
$(".QCMoreBtn").click(function(){
    $(this).hide();
    $(this).parent().find(".QCMoreBtnClose").show();
    $(this).parent().find("font").css("height","auto");
})

$(".QCMoreBtnClose").click(function(){
    $(this).hide();
    $(this).parent().find(".QCMoreBtn").show();
    $(this).parent().find("font").css("height","34px");
})

//质量控制日历
$(document).ready(function() {
	var fieldWidth = 0;
	$("#fieldoption").children().each(function(i, n) {
		fieldWidth += ($(n).width() + 60);
	});
	if (fieldWidth <= 575) {
		$("#fieldoption").next().hide();
	}

	var scopeWidth = 0;
	$("#scopeoption").children().each(function(i, n) {
		scopeWidth += ($(n).width() + 60);
	});
	if (scopeWidth > 575) {
		$("#scopeoption").next().show();
	}

	var natureWidth = 0;
	$("#natureoption").children().each(function(i, n) {
		natureWidth += ($(n).width() + 60);
	});
	if (natureWidth > 575) {
		$("#natureoption").next().show();
	}

	var countWidth = 0;
	$("#countoption").children().each(function(i, n) {
		countWidth += ($(n).width() + 60);
	});
	if (countWidth > 575) {
		$("#countoption").next().show();
	}
})

//质量控制上传

$("#QCCUpload").click(function(){
	$(".QCCAlert").fadeOut(100);
	var flag = true;
    flag = checkProjectName();
    flag = checkStartDate();
    flag = checkEndDate();
    flag = checkFeedBackDate();
    flag = checkPrice();
    flag = checkStandard();
    flag = checkDescription();
    
	if (flag)
		$('#qualityCtrlForm').submit();
});

$("#QCCCancel").click(function(){
    $(".QCCAlert").fadeOut(100);
})

$(".QCCSubmitBtn").click(function(){
    $(".QCCAlert").fadeIn(100);
})

//自定义 回调函数  
function ajaxCallBack(data) { 
     var result = data.rows;  
     var insetViewData = ""; //视图数据  
     $.each(result, function(index,items) {  
    	 //alert(items.id);  
    	 insetViewData += createQCs(items);  
     });  
     if (insetViewData == "")
	 {
		 insetViewData = "<div style='text-align:center;padding-top:140px;'>没有检索到数据</div>";
	 }
     $("#qclist").html(insetViewData);
}  

function createQCs(obj){
	var statusClass = "";
	if (obj.statusName == "未开始")
	{
		statusClass = "QCStateNotReady";
	} else if (obj.statusName == "等待反馈")
	{
		statusClass = "QCStateWaiting";
	} else if (obj.statusName == "正在进行")
	{
		statusClass = "QCStateMaking";
	} else if (obj.statusName == "已结束")
	{
		statusClass = "QCStateEnd";
	}
	var stateReg = "";
	if (obj.myQc == true)
	{
		stateReg = '<span class="QCStateReg">已报名</span>';
	}

	var qc = '<a href="' + $("#basePath").val() + 'qualitycontrol_contentShow.action?qualityId='
		+ obj.qcId + '" class="NCNewsListItem">'
		+ '<div class="QCItemLeft">'
		+ '<span class="QCName">' + obj.projectName.substr(0, 36) + '</span>'
		+ '<span class="QCAttr">' + obj.fieldName + '</span>'
		+ '<span class="QCAttr">' + obj.scopeName + '</span>'
		+ '<span class="QCAttr">' + obj.countName + '</span>'
		+ '<span class="QCAttr">' + obj.natureName + '</span>'
		+ '<span class="QCContent">' + obj.description.substr(0, 68)  +'</span>'
		+ '</div>'
		+ '<div class="QCItemRight">'
		+ stateReg
		+ '<span class="' + statusClass + '">' + obj.statusName + '</span>'
		+ '<span class="QCDate">开始时间：' + obj.beginDate.substr(0, 10) + '</span>'
		+ '<span class="QCDate2">反馈时间：' + obj.feedBackDate.substr(0, 10) + '</span>'
		+ '</div>'
		+ '</a>'
	return qc;  
}

function setField(index) {
	$("#s_field").val(index);
	goSearchUrl();
}

function setScope(index) {
	$("#s_scope").val(index);
	goSearchUrl();
}

function setNature(index) {
	$("#s_nature").val(index);
	goSearchUrl();
}

function setCount(index) {
	$("#s_count").val(index);
	goSearchUrl();
}
$(".SearchBtn").click(function(){
	goSearchUrl();
})

function goSearchUrl() {
	url = $("#basePath").val() + "qualitycontrol_index.action?s_field=" + $("#s_field").val() 
	+ "&s_scope=" + $("#s_scope").val() + "&s_count=" + $("#s_count").val() + "&s_nature=" + $("#s_nature").val() 
	+ "&s_qcName=" + $("#s_qcName").val();
	url = encodeURI(encodeURI(url));
    location.href = url;
}

$("#projectName").blur(function() {
	checkProjectName();
});

$("#beginDate").blur(function() {
	//checkStartDate();
	$("#beginDate").parent().find(".RegMsg").hide();
});

$("#endDate").blur(function() {
	//checkEndDate();
	$("#endDate").parent().find(".RegMsg").hide();
});

$("#feedBackDate").blur(function() {
	//checkFeedBackDate();
	$("#feedBackDate").parent().find(".RegMsg").hide();
});

$("#price").blur(function() {
	checkPrice();
});

$("#standard").blur(function() {
	checkStandard();
});

$("#description").blur(function() {
	checkDescription();
});

function checkProjectName() {
    if($("#projectName").val() == "")
    {
        $("#projectName").parent().find(".RegMsg").show();
        return false;
    }
    else
    {
        $("#projectName").parent().find(".RegMsg").hide();
        return true;
    }
}

function checkStartDate() {
    if($("#beginDate").val() == "")
    {
        $("#beginDate").parent().find(".RegMsg").show();
        return false;
    }
    else
    {
        $("#beginDate").parent().find(".RegMsg").hide();
        return true;
    }
}

function checkEndDate() {
    if($("#endDate").val() == "")
    {
        $("#endDate").parent().find(".RegMsg").show();
        return false;
    }
    else
    {
        $("#endDate").parent().find(".RegMsg").hide();
        return true;
    }
}

function checkFeedBackDate() {
    if($("#feedBackDate").val() == "")
    {
        $("#feedBackDate").parent().find(".RegMsg").show();
        return false;
    }
    else
    {
        $("#feedBackDate").parent().find(".RegMsg").hide();
        return true;
    }
}

function checkPrice() {
    if($("#price").val() == "")
    {
        $("#price").parent().find(".RegMsg").html("价格不能为空!").show();
        return false;
    }
    else if (isNaN($("#price").val()))
    {
        $("#price").parent().find(".RegMsg").html("价格必须为数字!").show();
        return true;
    }
    else
    {
        $("#price").parent().find(".RegMsg").hide();
        return true;
    }
}

function checkStandard() {
    if($("#standard").val() == "")
    {
        $("#standard").parent().find(".RegMsg").show();
        return false;
    }
    else
    {
        $("#standard").parent().find(".RegMsg").hide();
        return true;
    }
}

function checkDescription() {
    if($("#description").val() == "")
    {
        $("#description").parent().find(".RegMsg").show();
        return false;
    }
    else
    {
        $("#description").parent().find(".RegMsg").hide();
        return true;
    }
}

//js本地图片预览，兼容ie[6-9]、火狐、Chrome17+、Opera11+、Maxthon3
function PreviewImage(fileObj, imgPreviewId, divPreviewId) {
    var allowExtention = ".jpg,.bmp,.gif,.png"; //允许上传文件的后缀名document.getElementById("hfAllowPicSuffix").value;
    var extention = fileObj.value.substring(fileObj.value.lastIndexOf(".") + 1).toLowerCase();
    var browserVersion = window.navigator.userAgent.toUpperCase();
    if (allowExtention.indexOf(extention) > -1) {
        if (fileObj.files) {//HTML5实现预览，兼容chrome、火狐7+等
            if (window.FileReader) {
                var reader = new FileReader();
                reader.onload = function (e) {
					$("#" + imgPreviewId).attr("src", e.target.result).css("width", $(this).width > 300 ? "300px" : $(this).width);
                }
                reader.readAsDataURL(fileObj.files[0]);
            } else if (browserVersion.indexOf("SAFARI") > -1) {
                alert("不支持Safari6.0以下浏览器的图片预览!");
            }
        } else if (browserVersion.indexOf("MSIE") > -1) {
            if (browserVersion.indexOf("MSIE 6") > -1) {//ie6
                document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);
            } else {//ie[7-9]
                fileObj.select();
                if (browserVersion.indexOf("MSIE 9") > -1)
                    fileObj.blur(); //不加上document.selection.createRange().text在ie9会拒绝访问
                var newPreview = document.getElementById(divPreviewId + "New");
                if (newPreview == null) {
                    newPreview = document.createElement("div");
                    newPreview.setAttribute("id", divPreviewId + "New");
                    //newPreview.style.width = document.getElementById(imgPreviewId).width + "px";
                    //newPreview.style.height = document.getElementById(imgPreviewId).height + "px";
                    document.getElementById(imgPreviewId).style.display = "none";
                    newPreview.style.width = 300 + "px";
                    newPreview.style.height = 200 + "px";
                    newPreview.style.border = "solid 1px #d2e2e2";
                }
                newPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + document.selection.createRange().text + "')";
                var tempDivPreview = document.getElementById(divPreviewId);
                tempDivPreview.parentNode.insertBefore(newPreview, tempDivPreview);
                tempDivPreview.style.display = "none";
            }
        } else if (browserVersion.indexOf("FIREFOX") > -1) {//firefox
            var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(/firefox\/([\d.]+)/)[1]);
            if (firefoxVersion < 7) {//firefox7以下版本
                document.getElementById(imgPreviewId).setAttribute("src", fileObj.files[0].getAsDataURL());
            } else {//firefox7.0+                    
                document.getElementById(imgPreviewId).setAttribute("src", window.URL.createObjectURL(fileObj.files[0]));
            }
        } else {
            document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);
        }
    } else {
        alert("仅支持" + allowExtention + "为后缀名的文件!");
        fileObj.value = ""; //清空选中文件
        if (browserVersion.indexOf("MSIE") > -1) {
            fileObj.select();
            document.selection.clear();
        }
        fileObj.outerHTML = fileObj.outerHTML;
    }
    return fileObj.value;    //返回路径
}